'use strict';

/** @type {import('sequelize-cli').Migration} */
module.exports = {
  async up(queryInterface, Sequelize) {
    // 删除所有可能存在的唯一索引
    try {
      await queryInterface.sequelize.query('DROP INDEX unique_friendship_with_status ON friendships');
    } catch (error) {
      console.log('Index unique_friendship_with_status not found');
    }
    
    try {
      await queryInterface.sequelize.query('DROP INDEX unique_friendship ON friendships');
    } catch (error) {
      console.log('Index unique_friendship not found');
    }
    
    try {
      await queryInterface.sequelize.query('DROP INDEX unique_accepted_friendship ON friendships');
    } catch (error) {
      console.log('Index unique_accepted_friendship not found');
    }
    
    // 添加新的唯一约束
    await queryInterface.addConstraint('friendships', {
      fields: ['userId', 'friendId'],
      type: 'unique',
      name: 'unique_friendship'
    });
  },

  async down(queryInterface, Sequelize) {
    // 删除新的唯一约束
    await queryInterface.removeConstraint('friendships', 'unique_friendship');
    
    // 恢复旧的唯一约束
    await queryInterface.addConstraint('friendships', {
      fields: ['userId', 'friendId', 'status'],
      type: 'unique',
      name: 'unique_friendship_with_status'
    });
  }
};
